package com.example.lbs.service;

import com.example.lbs.mapper.ShopMapper;
import com.example.lbs.model.Shop;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class ShopService {
    private final ShopMapper mapper;
    private final ShopGeoService geoService;
    private final ShopCacheService cacheService;

    public ShopService(ShopMapper mapper, ShopGeoService geoService, ShopCacheService cacheService) {
        this.mapper = mapper;
        this.geoService = geoService;
        this.cacheService = cacheService;
    }

    @Transactional
    public Shop create(Shop shop) {
        mapper.insert(shop);
        // 事务提交后再更新 Redis GEO 与缓存
        org.springframework.transaction.support.TransactionSynchronizationManager.registerSynchronization(
            new org.springframework.transaction.support.TransactionSynchronization() {
                @Override
                public void afterCommit() {
                    try {
                        geoService.addShopGeo(shop.getId(), shop.getLng(), shop.getLat(), shop.getBrand());
                        cacheService.cacheShopDetail(shop);
                    } catch (Exception e) {
                        log.error("Failed to update cache after transaction commit", e);
                        // 可以在此处添加补偿逻辑，例如重试或记录失败状态
                    }
                }
            }
        );
        return shop;
    }

    @Transactional
    public Shop update(Shop shop) {
        Shop old = mapper.findById(shop.getId());
        mapper.update(shop);
        org.springframework.transaction.support.TransactionSynchronizationManager.registerSynchronization(
            new org.springframework.transaction.support.TransactionSynchronization() {
                @Override
                public void afterCommit() {
                    try {
                        // 先从旧品牌集合移除，再加入新品牌与全量
                        if (old != null) {
                            geoService.removeShopGeo(old.getId(), old.getBrand());
                        }
                        geoService.addShopGeo(shop.getId(), shop.getLng(), shop.getLat(), shop.getBrand());
                        cacheService.cacheShopDetail(shop);
                    } catch (Exception ignored) {}
                }
            }
        );
        return shop;
    }

    @Transactional
    public void delete(Long id) {
        Shop old = mapper.findById(id);
        mapper.delete(id);
        org.springframework.transaction.support.TransactionSynchronizationManager.registerSynchronization(
            new org.springframework.transaction.support.TransactionSynchronization() {
                @Override
                public void afterCommit() {
                    try {
                        if (old != null) {
                            geoService.removeShopGeo(old.getId(), old.getBrand());
                            cacheService.delete(old.getId());
                        }
                    } catch (Exception ignored) {}
                }
            }
        );
    }

    public Shop findById(Long id) {
        return mapper.findById(id);
    }

    public List<Shop> listAll() {
        return mapper.findAll();
    }
}